const { DataTypes } = require('sequelize');
const sequelize = require('../../config/db');
const Category = require('./Category');
const ProductCategory = require('./ProductCategory');

const Product = sequelize.define('product', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING, allowNull: false },
  price: { type: DataTypes.DECIMAL(10,2), allowNull: false },
  image: { type: DataTypes.STRING },
  logo: { type: DataTypes.STRING }, // 商品logo图
  images: { type: DataTypes.TEXT }, // 商品详情图（JSON数组字符串）
  stock: { type: DataTypes.INTEGER, defaultValue: 0 },
  description: { type: DataTypes.TEXT },
  category_id: { type: DataTypes.INTEGER, references: { model: Category, key: 'id' } },
  user_category_id: { type: DataTypes.INTEGER },
  created_at: { type: DataTypes.DATE, defaultValue: DataTypes.NOW }
}, {
  tableName: 'product',
  timestamps: false
});

Product.belongsTo(ProductCategory, { foreignKey: 'category_id', as: 'product_category' });
Product.belongsTo(Category, { foreignKey: 'user_category_id', as: 'user_category' });

module.exports = Product;
